/** StopWatch encapsulates calls to the performance timers
**
** @version: $Id: StopWatch.cpp 64 2010-11-25 20:46:39Z NeurobaticsTim@gmail.com $
*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "StopWatch.h"


/*****************************************************\
* Constructor                                         *
\*****************************************************/
StopWatch::StopWatch()
{
	__int64 i64CountsPerSecond = 0;

	/* Save number of ticks per second */
	if (!QueryPerformanceFrequency((LARGE_INTEGER*)&i64CountsPerSecond))
	{
		throw "Error: no time base!";
	}
	dSecondsPerCount = 1.0 / (double)i64CountsPerSecond;
	QueryPerformanceCounter((LARGE_INTEGER*)&i64Then);
}


/*****************************************************\
* Destructor                                          *
\*****************************************************/
StopWatch::~StopWatch()
{
}


/*****************************************************\
* Get time since previous call                        *
\*****************************************************/
double StopWatch::GetDeltaT()
{
	__int64 i64Now = 0;

	/* What time is it now? */
	QueryPerformanceCounter((LARGE_INTEGER*)&i64Now);
	/* Subtract off old time */
	double dRetVal = (i64Now - i64Then) * dSecondsPerCount;
	/* Next call, now will be the old time */
	i64Then = i64Now;

#ifdef _DEBUG
	/* When debugging, don't let the elapsed time get too long */
	dRetVal = min(0.1f, dRetVal);
#endif
	return dRetVal;
}


/*****************************************************\
* Reset                                               *
\*****************************************************/
void StopWatch::Reset()
{
	QueryPerformanceCounter((LARGE_INTEGER*)&i64Then);
}
